//
//  DoctorSelectTableViewController.m
//  CareLink
//
//  Created by apple on 16/8/19.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "DoctorSelectTableViewController.h"
#import "CLDoctorTableViewCell.h"
#import "CLNetworkManager.h"
#import "DoctorSelectModel.h"
#import "DoctorBasicViewController.h"

static NSString *cellid = @"cellid";
@interface DoctorSelectTableViewController ()

@property (nonatomic, strong)  CLNetworkManager *networkManager;
/**
 *  医生数据
 */
@property (nonatomic,strong) NSArray *DoctorData;

@end

@implementation DoctorSelectTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 预估行高
    self.tableView.estimatedRowHeight = 100;
    // 自动计算
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    // 取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 加载网络数据
    [self matchDoctors];
    // 设置数据源
    [self.tableView registerNib:[UINib nibWithNibName:@"CLDoctorTableViewCell" bundle:nil] forCellReuseIdentifier:cellid];
    // 方式一
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    // 设置刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(matchDoctors)];
    // 取消页面的滚动条效果
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    // 设置navgatationcontroller的title
    self.navigationItem.title = @"医生选择";


}

//{
//    ci1_id : 1,
//    diagnosis_type : 0,
//    ci2_id : 3,
//    ci3_id : 3,
//    diseaseSubject : 肿瘤,
//    is_confirmed : 1,
//    complications : [
//                     黑粪,
//                     胃穿孔,
//                     呕血,
//                     缺铁性贫血1
//                     ],
// 获取网络数据
- (void)matchDoctors {
//    开始刷新
    [self.tableView.mj_header beginRefreshing];
//    [self.DoctorData ]
    NSString *url = @"http://iosapi.itcast.cn/matchDoctors.json.php";
    //提取特征信息
    NSString *ci1_id = self.selectDictData[@"ci1_id"];
    NSString *ci2_id = self.selectDictData[@"ci2_id"];
    NSString *ci3_id = self.selectDictData[@"ci3_id"];
    NSString *diagnosis_type = self.selectDictData[@"diagnosis_type"];
    NSString *is_confirmed = self.selectDictData[@"is_confirmed"];
    NSDictionary *complications = self.selectDictData[@"complications"];
    NSString *has_diagnosis = self.selectDictData[@"has_diagnosis"];
    NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f",@"ci1_id":ci1_id,@"ci2_id":ci2_id,@"ci3_id":ci3_id,@"diagnosis_type":diagnosis_type,@"is_confirmed":is_confirmed,@"user_id":@(1000089),@"complications":complications,@"has_diagnosis":has_diagnosis};
    // 网络请求
    [self.networkManager POST:url parameters:paramDic success:^(id responseObject) {
        // 获得网络数据并转成model
        NSArray *DoctorData = [NSArray yy_modelArrayWithClass:[DoctorSelectModel class] json:[self jiexishuju:responseObject]];
        // 数据回来关闭刷新
        [self.tableView.mj_header endRefreshing];
        // 赋值
        self.DoctorData = DoctorData;
        // 数据回来刷新单元格
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        // 数据错误关闭刷新
        [self.tableView.mj_header endRefreshing];
        // 提示错误
        [SVProgressHUD showErrorWithStatus:@"服务器傲娇了！！" maskType:SVProgressHUDMaskTypeBlack];
        
        NSLog(@"数据错误");
    }];
}

-(NSArray *)jiexishuju:(NSData *)responseObject
{
    NSString *str =  [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
    //判断字符串的前缀是不是@"<br />"，如果不是数据错误，直接返回空字典
    if (![str hasPrefix:@"<br />"]) {
        return [NSArray array];
    }
    
    //判断 结束标志的位置
    NSRange range = [str rangeOfString:@"<br />" options:NSBackwardsSearch];
    
    //需要数据的下标
    NSInteger len = range.length+range.location+1;
    
    //截取 验证字符串 后面的部分,得到想要的数据
    NSString *string66 = [str substringFromIndex:len];
    
    NSData *jsonData = [string66 dataUsingEncoding:NSUTF8StringEncoding];//转化为data
    NSDictionary *data = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];//转化为字典
    
    return data[@"data"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

// 懒加载网络manager
- (CLNetworkManager *)networkManager {
    
    if (_networkManager == nil) {
        
        _networkManager = [CLNetworkManager sharedManager];
        
    }
    return _networkManager;
    
    
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.DoctorData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLDoctorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = self.DoctorData[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DoctorBasicViewController *basicViewController = [[DoctorBasicViewController alloc]init];
    basicViewController.sickDataDict = self.selectDictData;
    basicViewController.selectModel = self.DoctorData[indexPath.row];
    [self.navigationController pushViewController:basicViewController animated:YES];
}
- (void)dealloc
{
    // controller 销毁的时候，取消所有正在下载的线程
    [self.networkManager cancelOperations];
    NSLog(@"DoctorSelectTableViewController销毁了");

}

@end
